package com.song.controller;


import com.alibaba.nacos.api.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.song.Dto.UserDto;
import com.song.VO.UserVo;
import com.song.base.consts.ResponseEnum;
import com.song.base.model.ResponseEntity;
import com.song.base.util.RedisKeys;
import com.song.domain.Tuser;
import com.song.service.TuserService;
import com.song.util.PassUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.regex.Pattern;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author Song
 * @since 2022-12-19
 */
@Api(tags = "用户信息控制")
@Slf4j
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("user")
public class TuserController {
    @Resource
    private TuserService tuserService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @ApiOperation("通过邮箱查询用户")
    @GetMapping("byEmail")
    public UserVo queryUserByEmail(String email){
        log.info("TuserController的queryUserByEmail邮箱验证进入{}",email);
        Tuser tuser = tuserService.queryUserByEmail(email);
        if (tuser!=null) {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(tuser, userVo);
            return userVo;
        }
        return null;
    }
    @ApiOperation("通过手机号查询用户")
    @GetMapping("byTel")
    public UserVo queryUserByTel(String tel){
        Tuser tuser = tuserService.queryUserByTel(tel);
        if (tuser!=null) {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(tuser, userVo);
            return userVo;
        }
        return null;
    }
    @ApiOperation("新增用户")
    @PostMapping("addUser")
    public ResponseEntity addUser(@RequestBody UserVo userVo){
        log.info("TuserController的addUser新增用户参数{}",userVo);
        ResponseEntity<Object> responseEntity = new ResponseEntity<>();
        if (StringUtils.isBlank(userVo.getCname())||StringUtils.isBlank(userVo.getPassword())){
            responseEntity.setMsg("用户名或密码不能为空");
            responseEntity.setCode(ResponseEnum.CHECK_FLOW.getCode());
        }else if (queryUserByEmail(userVo.getMailbox())!=null){
            responseEntity.setMsg("邮箱已存在");
            responseEntity.setCode(ResponseEnum.CHECK_FLOW.getCode());
//        }else if(queryUserByTel(userVo.getTel())!=null){
//            responseEntity.setMsg("手机号已存在");
//            responseEntity.setCode(ResponseEnum.CHECK_FLOW.getCode());
        }else {
            ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
            String checkR = opsForValue.get(RedisKeys.USER_EMAIL_CHECK + userVo.getMailbox());
            if (userVo.getCheck().equals(checkR)){
                stringRedisTemplate.delete(RedisKeys.USER_EMAIL_CHECK + userVo.getMailbox());
                Tuser tuser = new Tuser();
                tuser.setSex(userVo.getSexB()?1:0);
                BeanUtils.copyProperties(userVo,tuser);
                //密码加密
                tuser.setPassword(PassUtil.PasssWitch(tuser.getPassword()+"LS"));
                tuserService.save(tuser);
                responseEntity.setMsg("重生成功");
                responseEntity.setCode(ResponseEnum.RES_SUCCESS.getCode());
            }else {
                responseEntity.setMsg("验证码错误");
                responseEntity.setCode(ResponseEnum.CHECK_FLOW.getCode());
            }
        }
        return responseEntity;
    }
    @ApiOperation("使用邮箱通过密码(1)或者验证码(3)登录")
    @GetMapping("login")
    public ResponseEntity login(String emailTel,int type,String pass){
        log.info("TuserController的login登录用户参数{}-类型{}—{}",emailTel,type,pass);
        ResponseEntity<Object> responseEntity = new ResponseEntity<>();
        QueryWrapper<Tuser> queryWrapper = new QueryWrapper<>();
        if(type==3){//验证码登录
            if (StringUtils.isBlank(pass)||StringUtils.isBlank(emailTel)){
                responseEntity.setCode(ResponseEnum.CHECK_FLOW.getCode());
                responseEntity.setMsg("邮箱和验证码不能为空");
            }else {
                queryWrapper.eq("mailbox",emailTel);
                Tuser one = tuserService.getOne(queryWrapper);
                if (one==null){
                    responseEntity.setCode(ResponseEnum.CHECK_FLOW.getCode());
                    responseEntity.setMsg("邮箱不存在请先去注册欧");
                }else {
                    ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
                    String checkR = opsForValue.get(RedisKeys.USER_EMAIL_LOGIN + emailTel);
                    if (pass.equals(checkR)){
                        stringRedisTemplate.delete(RedisKeys.USER_EMAIL_LOGIN + emailTel);
                        UserDto userDto = tuserService.login(one);
                        responseEntity.setCode(ResponseEnum.RES_SUCCESS.getCode());
                        responseEntity.setMsg("登录成功");
                        responseEntity.setData(userDto);
                    }else {
                        responseEntity.setMsg("验证码错误");
                        responseEntity.setCode(ResponseEnum.CHECK_FLOW.getCode());
                    }
                }
            }
        }else {
            queryWrapper.eq("password", PassUtil.PasssWitch(pass+"LS"));
            if (!StringUtils.isBlank(pass) && (!StringUtils.isBlank(emailTel))) {
                if (type == 1) {
                    queryWrapper.eq("mailbox", emailTel);
                } else if (type == 2) {
                    queryWrapper.eq("tel", emailTel);
                }
                Tuser one = tuserService.getOne(queryWrapper);
                if (one != null) {
                    UserDto userDto = tuserService.login(one);
                    responseEntity.setCode(ResponseEnum.RES_SUCCESS.getCode());
                    responseEntity.setMsg("登录成功");
                    responseEntity.setData(userDto);
                } else {
                    responseEntity.setMsg("用户名或密码错误");
                    responseEntity.setCode(ResponseEnum.QUERY_FLOW.getCode());
                }
            } else {
                responseEntity.setMsg("用户名或密码错误");
                responseEntity.setCode(ResponseEnum.QUERY_FLOW.getCode());
            }
        }
        return responseEntity;
    }
    @ApiOperation("发送验证码,1登录2注册")
    @CrossOrigin(origins = "*")
    @GetMapping("sendEmail")
    public ResponseEntity sendEmail(String email,int type){
        log.info("TuserController的sendEmail发送邮箱参数{}",email);
        ResponseEntity<Object> responseEntity = new ResponseEntity<>();
        String regex="^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$";
        if(Pattern.matches(regex,email)) {
            String s = tuserService.sendEmail(email,type);
            if (s != null) {
                responseEntity.setMsg(s);
                responseEntity.setCode(ResponseEnum.RES_SUCCESS.getCode());
            }
        }else {
            responseEntity.setMsg("邮箱格式异常请检查");
            responseEntity.setCode(ResponseEnum.RES_ERROR.getCode());
        }
        return responseEntity;
    }
    @ApiOperation("连通测试")
    @GetMapping("aa")
    public String aa(){
        return "测试成功";
    }
}

